package com.niuke;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 41. 最长广播响应
 * https://blog.csdn.net/weixin_48157259/article/details/131434335
 */
public class NiukeMoni41 {

    public static Set<Integer> nodes = new HashSet<>();
    public static Map<Integer, Set<Integer>> data = new HashMap<>();
    public static int result = 0;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String[] line1 = scanner.nextLine().split(",");

        int nodeLength = Integer.parseInt(line1[0]);
        int dataLines = Integer.parseInt(line1[1]);

        for (int i = 0; i < nodeLength; i++) {
            nodes.add(i + 1);
        }

        for (int i = 0; i < dataLines; i++) {
            String[] guanXi = scanner.nextLine().split(",");
            int key = Integer.parseInt(guanXi[0]);
            int value = Integer.parseInt(guanXi[1]);

            Set<Integer> item = data.getOrDefault(key, new HashSet<>());
            item.add(value);
            data.put(key, item);
        }

        int startNode = Integer.parseInt(scanner.nextLine());

        Set<Integer> starts = new HashSet<>();
        starts.add(startNode);
        dfs(starts);

        System.out.println(2 * result - 2);
    }

    public static void dfs(Set<Integer> keys) {
        if (nodes.size() == 0) {
            return;
        }

        nodes.removeAll(keys);
        result++;

        Set<Integer> newKeys = new HashSet<>();
        for (Integer currKey : keys) {
            Set<Integer> item = data.get(currKey);
            if (item != null && item.size() > 0) {
                newKeys.addAll(item);
            }
        }
        dfs(newKeys);
    }

}
